package com.zhongxi.guoyuan.view.user;

import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.adapter.DividendDetailAdapter;
import com.zhongxi.guoyuan.application.MyApplication;
import com.zhongxi.guoyuan.base.BaseActivity;
import com.zhongxi.guoyuan.entity.BaseResponse;
import com.zhongxi.guoyuan.entity.gift.GiftRuleResult;
import com.zhongxi.guoyuan.entity.user.DividendRecorderResult;
import com.zhongxi.guoyuan.entity.user.TodayEarningResult;
import com.zhongxi.guoyuan.url.Url;
import com.zhongxi.guoyuan.utils.DataUtil;
import com.zhongxi.guoyuan.utils.GlideRoundTransform;
import com.zhongxi.guoyuan.view.HtmlActivity;
import com.zhongxi.guoyuan.view.MainHomeActivity;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import static com.zhongxi.guoyuan.R.id.middle;
import static com.zhongxi.guoyuan.R.id.start;
import static com.zhongxi.guoyuan.R.id.userInfo;

//import android.support.annotation.RequiresApi;

public class MyDividendActivity extends BaseActivity implements View.OnClickListener, PullToRefreshBase.OnRefreshListener2<ListView> {

    //    private ImageView ivShare;
//    private ImageView ivGetCash;
    private ImageView ivLingqu;
    private LinearLayout linearInfo;
    private PullToRefreshListView lvList;
    private ImageView ivImg;
    private TextView tvName;
    private TextView tvPhone;
    private TextView tvPoint;
    private TextView tvMoney;
    private TextView tvTotalMoney;
    private TextView tvNoGet;
    private TextView tvRedBean;
    private TextView divided_beautyBean;
    private LinearLayout linearDivide;
    private TextView tvPatnerShares;
    //豆豆说明
    private LinearLayout linearDeclare;

    private int stateLeft = 0;
    private int stateRight = 0;
    private List<DividendRecorderResult.RecodeCoinRecordDetailResult> data = new ArrayList<>();
    private DividendDetailAdapter adapter;
    private String flag;
    private TextView tvTime;
    private ImageView ivFenrun;

    private int useType;

    @Override
    protected void initView() {
        super.initView();
        setContentView(R.layout.activity_my_dividend);
        initTitle();
        tvTitle.setText("昨日分润收益");
        ivLingqu = (ImageView) findViewById(R.id.record_lingqu);
        ivFenrun = (ImageView) findViewById(R.id.record_fenrun);
        linearInfo = (LinearLayout) findViewById(R.id.linearInfo);
        lvList = (PullToRefreshListView) findViewById(R.id.lvList);
        lvList.setMode(PullToRefreshBase.Mode.BOTH);
        lvList.setVisibility(View.GONE);

        linearDeclare = (LinearLayout) findViewById(R.id.divided_doudou_declare);
        //用户信息
        ivImg = (ImageView) findViewById(R.id.ivImg);
        tvName = (TextView) findViewById(R.id.tvName);
        tvTime = (TextView) findViewById(R.id.tvTime);
        tvPhone = (TextView) findViewById(R.id.tvPhone);
        tvPoint = (TextView) findViewById(R.id.tvPoint);
        tvMoney = (TextView) findViewById(R.id.tvMoney);
        tvTotalMoney = (TextView) findViewById(R.id.tvTotalMoney);
        tvNoGet = (TextView) findViewById(R.id.tvNoGet);
        tvRedBean = (TextView) findViewById(R.id.divided_redBean);
        divided_beautyBean = (TextView) findViewById(R.id.divided_beautyBean);  //美豆
        linearDivide = (LinearLayout) findViewById(R.id.linear_divide_partner);
        tvPatnerShares = (TextView) findViewById(R.id.tvPartnerShares);
        if (null != MyApplication.getInstance().getInnerCurrency() && !"".equals(MyApplication.getInstance().getInnerCurrency())) {
            divided_beautyBean.setText("美豆量：" + MyApplication.getInstance().getInnerCurrency());
        } else {
            divided_beautyBean.setText("美豆量：0 ");
        }
    }

    @Override
    protected void initData() {
        super.initData();
        initTime();
        flag = "today";
        getServer(Url.MY_TODAY_EARNINGS, null, "", false, null, "get");
    }

    private void initTime() {
        Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH) + 1;
        int day = c.get(Calendar.DAY_OF_MONTH);
        tvTime.setText("今天是" + year + "年" + month + "月" + day + "日");
    }

    @Override
    protected void processSuccessResult(String response) {
        super.processSuccessResult(response);
        Gson gson = new Gson();
        try {
            if ("today".equals(flag)) {
                TodayEarningResult result = gson.fromJson(response, TodayEarningResult.class);
                MyApplication.getInstance().setSpValue(result.getSafeobj());
                initInfo(result);
            } else if ("recorder".equals(flag)) {
                DividendRecorderResult result = gson.fromJson(response, DividendRecorderResult.class);
                MyApplication.getInstance().setSpValue(result.getSafeobj());
                if (pageNum == 1) {
                    data.clear();
                }
                if (result.getRecode().size() > 0) {
                    data.addAll(result.getRecode());
                    adapter = new DividendDetailAdapter(context, data);
                    lvList.setAdapter(adapter);
                }
                adapter.notifyDataSetChanged();
                lvList.onRefreshComplete();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 初始化数据
     *
     * @param result
     */
    private void initInfo(TodayEarningResult result) {
        if (null != result.getUserInfo() && !"".equals(result.getUserInfo())) {
            TodayEarningResult.UserInfoCoinRecordDetailResult userInfo = result.getUserInfo();
            Glide.with(context).load(Url.BASE_URL + userInfo.getAvatar()).placeholder(R.mipmap.logo).into(ivImg);
            if (null != userInfo.getUsername() && !"".equals(userInfo.getUsername())) {
                tvName.setText("用户名：" + userInfo.getUsername());
            } else {
                tvName.setText(R.string.app_name);
            }
            if (null != userInfo.getMphone() && !"".equals(userInfo.getMphone())) {
                tvPhone.setText("手机号：" + MyApplication.getInstance().parseSecutiryKey(userInfo.getMphone()));
            } else {
                tvPhone.setText("");
            }
        }
        if (null != result.getRecode() && (result.getRecode().size() > 0)) {
            if (null != result.getRecode().get(0).getSharesgains() && !"".equals(result.getRecode().get(0).getSharesgains())) {
                tvMoney.setText(DataUtil.formatDataFivePoint(result.getRecode().get(0).getSharesgains()) + "元");
            } else {
                tvMoney.setText("0");
            }
        }

        //当前红豆数量
        if (null != result.getMyredbean() && !"".equals(result.getMyredbean())) {
            tvRedBean.setText("红豆量：" + DataUtil.formatDataSixPoint(result.getMyredbean()));
        } else {
            tvRedBean.setText("红豆量：0 ");
        }

        if (null != result.getMyshares() && !"".equals(result.getMyshares())) {
            tvPoint.setText(result.getMyshares());
        } else {
            tvPoint.setText("0");
        }

        if (result.getRights() != null && result.getRights().size() > 0) {
            linearDivide.setVisibility(View.GONE);
            for (int mI = 0; mI < result.getRights().size(); mI++) {
                if (null != result.getRights().get(mI).getUsertype() && !"".equals(result.getRights().get(mI).getUsertype())) {
                    if (result.getRights().get(mI).getUsertype().equals("1")) {
                        tvPatnerShares.setText(result.getRights().get(mI).getSharesnum());
                    } else if (result.getRights().get(mI).getUsertype().equals("2")) {
                        tvPoint.setText(result.getRights().get(mI).getSharesnum());
                    }

                }
            }
        } else {
            linearDivide.setVisibility(View.GONE);
        }


        if (null != result.getTotalgains() && !"".equals(result.getTotalgains())) {
            tvTotalMoney.setText("目前累积收益:" + DataUtil.formatDataFivePoint(result.getTotalgains()) + " 元");
        } else {
            tvTotalMoney.setText("目前累积收益：0 元");
        }
        if (null != result.getLastgains() && !"".equals(result.getLastgains())) {
            tvNoGet.setText("[未领取收益: " + DataUtil.formatDataFivePoint(result.getLastgains()) + " 元]");
        } else {
            tvNoGet.setText("[未领取收益:0 元]");
        }
    }

    @Override
    protected void ProcessFailResult(String response) {
        super.ProcessFailResult(response);
        Gson gson = new Gson();
        BaseResponse result = gson.fromJson(response, BaseResponse.class);
        if (null != result.getSafeobj() && !"".equals(result.getSafeobj()))
            MyApplication.getInstance().setSpValue(result.getSafeobj());
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        ivBack.setOnClickListener(this);
        ivLingqu.setOnClickListener(this);
        lvList.setOnRefreshListener(this);
        ivFenrun.setOnClickListener(this);
        linearDeclare.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ivBack:
                finish();
                startAct(MainHomeActivity.class);
                break;
            case R.id.record_fenrun:
                pageNum = 1;
                if (stateRight == 1 && stateLeft == 0) {
                    stateRight = 0;
                    stateLeft = 1;
                    ivFenrun.setImageResource(R.mipmap.iv_record_frback);
                    ivLingqu.setImageResource(R.mipmap.iv_record_lq);
                    useType = 1;
                    getRecorder();
                } else if (stateLeft == 0) {
                    ivFenrun.setImageResource(R.mipmap.iv_record_frback);
                    ivLingqu.setImageResource(R.mipmap.iv_record_lq);

                    linearInfo.setVisibility(View.GONE);
                    lvList.setVisibility(View.VISIBLE);
                    useType = 1;
                    getRecorder();
                    stateLeft = 1;

                } else {
                    ivFenrun.setImageResource(R.mipmap.iv_record_fr);
                    linearInfo.setVisibility(View.VISIBLE);
                    lvList.setVisibility(View.GONE);
                    stateLeft = 0;
                }

                break;
            case R.id.record_lingqu:
                pageNum = 1;
                if (stateRight == 0 && stateLeft == 1) {
                    stateRight = 1;
                    stateLeft = 0;
                    ivFenrun.setImageResource(R.mipmap.iv_record_fr);
                    ivLingqu.setImageResource(R.mipmap.iv_record_lqback);
                    useType = 2;
                    getRecorder();
                } else if (stateRight == 0) {
                    ivFenrun.setImageResource(R.mipmap.iv_record_fr);
                    ivLingqu.setImageResource(R.mipmap.iv_record_lqback);

                    linearInfo.setVisibility(View.GONE);
                    lvList.setVisibility(View.VISIBLE);
                    useType = 2;
                    getRecorder();
                    stateRight = 1;

                } else {
                    ivLingqu.setImageResource(R.mipmap.iv_record_lq);
                    linearInfo.setVisibility(View.VISIBLE);
                    lvList.setVisibility(View.GONE);
                    stateRight = 0;
                }

                break;
            case R.id.divided_doudou_declare:

                Intent intent = new Intent(context, HtmlActivity.class);
                intent.putExtra("type", "doudouRule");
                startActivity(intent);
                break;
        }

    }


    /**
     * 获取历史收益记录
     */
    private void getRecorder() {
        flag = "recorder";
        String str = "&page=" + pageNum + "&useType=" + useType;
        getServer(Url.MY_HISTORY_SHARES_GAINS, null, str, false, null, "get");
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
        pageNum = 1;
        getRecorder();
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
        pageNum++;
        getRecorder();
    }
}
